package com.whl.common;

import com.whl.common.constants.EnumType;
import com.whl.common.exception.BusinessException;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Collection;
import java.util.Map;

public class Assert {

	public static void isTrue(boolean expression, String message) {
		isTrue(expression, null, message);
	}

	public static void isTrue(boolean expression, Integer code, String message) {
		if (!expression) {
			throw new BusinessException(code == null ? EnumType.ResponseType.FAIL.code() : code, message);
		}
	}

	public static void isNull(Object object, String message) {
		isNull(object, null, message);
	}

	public static void isNull(Object object, Integer code, String message) {
		if (object != null) {
			throw new BusinessException(code == null ? EnumType.ResponseType.FAIL.code() : code, message);
		}
	}

	public static void notNull(Object object, String message) {
		notNull(object, null, message);
	}

	public static void notNull(Object object, Integer code, String message) {
		if (object == null) {
			throw new BusinessException(code == null ? EnumType.ResponseType.FAIL.code() : code, message);
		}
	}

	public static void hasLength(String text, String message) {
		hasLength(text, null, message);
	}

	public static void hasLength(String text, Integer code, String message) {
		if (!StringUtils.hasLength(text)) {
			throw new BusinessException(code == null ? EnumType.ResponseType.FAIL.code() : code, message);
		}
	}

	public static void hasText(String text, String message) {
		hasText(text, null, message);
	}

	public static void hasText(String text, Integer code, String message) {
		if (!StringUtils.hasText(text)) {
			throw new BusinessException(code == null ? EnumType.ResponseType.FAIL.code() : code, message);
		}
	}

	public static void notEmpty(Object[] array, String message) {
		notEmpty(array, null, message);
	}

	public static void notEmpty(Object[] array, Integer code, String message) {
		if (ObjectUtils.isEmpty(array)) {
			throw new BusinessException(code == null ? EnumType.ResponseType.FAIL.code() : code, message);
		}
	}

	public static void noNullElements(Object[] array, String message) {
		noNullElements(array, null, message);
	}

	public static void noNullElements(Object[] array, Integer code, String message) {
		if (array != null) {
			for (Object element : array) {
				if (element == null) {
					throw new BusinessException(code == null ? EnumType.ResponseType.FAIL.code() : code, message);
				}
			}
		}
	}

	public static void empty(Collection<?> collection, String message) {
		empty(collection, null, message);
	}

	public static void empty(Collection<?> collection, Integer code, String message) {
		if (!CollectionUtils.isEmpty(collection)) {
			throw new BusinessException(code == null ? EnumType.ResponseType.FAIL.code() : code, message);
		}
	}

	public static void notEmpty(Collection<?> collection, String message) {
		notEmpty(collection, null, message);
	}

	public static void notEmpty(Collection<?> collection, Integer code, String message) {
		if (CollectionUtils.isEmpty(collection)) {
			throw new BusinessException(code == null ? EnumType.ResponseType.FAIL.code() : code, message);
		}
	}

	public static void onlyOne(Collection<?> collection, String message) {
		onlyOne(collection, null, message);
	}

	public static void onlyOne(Collection<?> collection, Integer code, String message) {
		notEmpty(collection, code, message);
		if (collection.size() != 1) {
			throw new BusinessException(code == null ? EnumType.ResponseType.FAIL.code() : code, message);
		}
	}

	public static void notEmpty(Map<?, ?> map, String message) {
		notEmpty(map, null, message);
	}

	public static void notEmpty(Map<?, ?> map, Integer code, String message) {
		if (CollectionUtils.isEmpty(map)) {
			throw new BusinessException(code == null ? EnumType.ResponseType.FAIL.code() : code, message);
		}
	}
	
	public static void main(String args[]) {
		Assert.isTrue(false, "订单状态错误 ，请联系管理员处理");
	}

}
